package com.ayoka.parenting.ui.meditation.detail;

import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.SeekBar;

import com.ayoka.parenting.R;
import com.ayoka.parenting.adapter.MeditationAudioAdapter;
import com.ayoka.parenting.adapter.MeditationBackgroundMusicAdapter;
import com.ayoka.parenting.bean.MeditationBean;
import com.ayoka.parenting.bean.MeditationDetailBean;
import com.ayoka.parenting.interf.SimpleSeekBarChangeListener;
import com.ayoka.parenting.mvp.BaseView;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.common.brvah.RecycleViewDivider;
import com.jincp.common.brvah.layoutmanager.MyLinearLayoutManager;
import com.jincp.common.dialog.BaseDialogBuilder;
import com.jincp.common.dialog.BaseDialogFragment;
import com.jincp.common.utils.SharedPreferenceUtil;
import com.jincp.common.utils.UIUtils;
import com.tencent.ticsdk.core.TICManager;

import java.util.HashMap;
import java.util.Map;

/**
 * =========================================================
 *
 * @ProjectName:
 * @ClassName: WhiteBoardDialog
 * @Description:
 * @Author: jincp
 * @CreateDate: 2019/9/19 16:18
 * =========================================================
 */
public class MeditationSettingDialog extends BaseDialogFragment {

    public static MeditationSettingDialog newInstance(Builder builder) {
        MeditationSettingDialog dialog = new MeditationSettingDialog();
        Bundle bundle = getArgumentBundle(builder);
        dialog.setArguments(bundle);
        return dialog;
    }

    private MeditationDetailBean meditationDetailBean;

    @Override
    protected void convertView(@Nullable final View container, final BaseDialogFragment dialogFragment) {
        //返回
        container.findViewById(R.id.iv_close).setOnClickListener(new CloseClickListener());
        // 冥想音乐
        if (meditationDetailBean != null && meditationDetailBean.meditation != null && meditationDetailBean.meditation.MeditationVoices != null) {
            RecyclerView audioRecyclerView = container.findViewById(R.id.recyclerView_audio);
            audioRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
            MeditationAudioAdapter audioAdapter = new MeditationAudioAdapter();
            audioAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    SharedPreferenceUtil.putInt("MeditationAudioIndex", position);
                    ((MeditationAudioAdapter) adapter).setSingleChoice(position);
                    MeditationBean.MeditationVoiceBean meditationVoiceBean = (MeditationBean.MeditationVoiceBean) adapter.getItem(position);
                    if (mDialogResultListener != null) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("Operation", "Audio");
                        map.put("Value", meditationVoiceBean);
                        mDialogResultListener.result(MeditationSettingDialog.this, map);
                    }

                }
            });
            audioAdapter.setNewData(meditationDetailBean.meditation.MeditationVoices);
            audioRecyclerView.setAdapter(audioAdapter);
            RecyclerView.ItemDecoration itemDecoration = new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                    int position = parent.getChildAdapterPosition(view);
                    outRect.top = UIUtils.dp2px(10);
                    outRect.bottom = UIUtils.dp2px(10);
                    if (position % 2 == 0) {
                        outRect.left = UIUtils.dp2px(20);
                        outRect.right = UIUtils.dp2px(10);
                    } else {
                        outRect.left = UIUtils.dp2px(10);
                        outRect.right = UIUtils.dp2px(20);
                    }
                }
            };
            audioRecyclerView.addItemDecoration(itemDecoration);
        }
        SeekBar seekBar = container.findViewById(R.id.sb_progress);
        seekBar.setMax(100);
        seekBar.setProgress(meditationDetailBean.backgroundMusicVolume);
        seekBar.setOnSeekBarChangeListener(new SimpleSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                meditationDetailBean.backgroundMusicVolume = progress;
                if (mDialogResultListener != null) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("Operation", "BackgroundMusicVolume");
                    map.put("Value", meditationDetailBean.backgroundMusicVolume / 100f);
                    mDialogResultListener.result(MeditationSettingDialog.this, map);
                }
            }
        });
        // 冥想背景音乐
        if (meditationDetailBean != null && meditationDetailBean.bg_musics != null) {
            RecyclerView musicRecyclerView = container.findViewById(R.id.recyclerView_background_music);
            musicRecyclerView.setLayoutManager(new MyLinearLayoutManager(getActivity()));
            MeditationBackgroundMusicAdapter musicAdapter = new MeditationBackgroundMusicAdapter();
            musicAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    SharedPreferenceUtil.putInt("BackgroundMusicIndex", position);
                    ((MeditationBackgroundMusicAdapter) adapter).setSingleChoice(position);
                    MeditationDetailBean.BgMusicsBean bgMusicsBean = (MeditationDetailBean.BgMusicsBean) adapter.getItem(position);
                    if (mDialogResultListener != null) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("Operation", "BackgroundMusic");
                        map.put("Value", bgMusicsBean);
                        mDialogResultListener.result(MeditationSettingDialog.this, map);
                    }

                }
            });
            musicAdapter.setNewData(meditationDetailBean.bg_musics);
            musicRecyclerView.setAdapter(musicAdapter);
        }
        container.findViewById(R.id.btn_save_setting).setOnClickListener(new CloseClickListener());
    }

    public void setMeditationDetailBean(MeditationDetailBean meditationDetailBean) {
        this.meditationDetailBean = meditationDetailBean;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_meditation_setting;
    }


    public static class Builder extends BaseDialogBuilder<Builder, MeditationSettingDialog> {

        @Override
        public MeditationSettingDialog build() {
            return MeditationSettingDialog.newInstance(this);
        }
    }
}